package org.example.sort.type;

import java.util.Comparator;

/**
 * 类描述：冒泡排序
 * <br>
 *
 * @author ZS
 * @version 1.0
 * @date 2020/12/28
 **/
public class BubbleSort1<E> extends AbstractSort<E> {
    public BubbleSort1() {
        super();
    }

    public BubbleSort1(Comparator<E> comparator) {
        super(comparator);
    }

    @Override
    protected E[] sort() {
        // 每循环一次，都有一位最大值被移至最后
        for (int end = elements.length - 1; end > 0; end--) {
            for (int j = 1; j <= end; j++) {
                if (compare(elements[j - 1], elements[j]) > 0) {
                    swap(j - 1, j);
                }
            }
        }
        return elements;
    }
}
